import sys
from PyQt5.QtWidgets import (
    QApplication,
    QWidget,
    QLineEdit,
    QLabel,
    QPushButton,
    QInputDialog,
)

from PasswdDialog import PassdDialog


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.init_UI()

    def init_UI(self):
        # self.setGeometry(300, 300, 380, 180)
        self.resize(380, 180)
        self.setWindowTitle("自定义密码输入对话框")
        self.lab1 = QLabel("密码在此显示...", self)
        self.lab1.move(40, 20)

        self.bt1 = QPushButton("输入密码(普通型)", self)
        self.bt1.move(20, 60)

        self.bt2 = QPushButton("输入密码(普通加强型)", self)
        self.bt2.move(20, 100)

        self.bt3 = QPushButton("输入密码(特别加强型)", self)
        self.bt3.move(20, 140)

        self.bt1.clicked.connect(self.showDialog)
        self.bt2.clicked.connect(self.showDialog)
        self.bt3.clicked.connect(self.showDialog)
        self.show()

    def showDialog(self):
        sender = self.sender()
        if sender == self.bt1:
            text, ok = QInputDialog.getText(
                self, "密码输入框", "请输入密码", QLineEdit.Password
            )
            if ok:
                self.lab1.setText(text)

        elif sender == self.bt2:
            text, ok = QInputDialog.getText(
                self, "密码输入框", "请输入密码：", QLineEdit.PasswordEchoOnEdit
            )
            if ok:
                self.lab1.setText(text)

        else:
            pwd = PassdDialog()
            r = pwd.exec_()
            if r:
                self.lab1.setText(pwd.text)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
